import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
	{
		path: "/",
		redirect: "/login"
		
	},
	{
		path: '/login',
		name: 'login',
		component: () => import('../views/Login.vue')
	},
	{
		path: '/allProject',
		name: 'allProject',
		component: () => import('../views/allProject.vue')
	},
	{
		path: '/home',
		name: 'home',
		component: () => import('../views/Home.vue'),
		redirect: "/project",
		children: [
			{
				path: '/project',
				name: 'project',
				component: () => import('../views/Project.vue')
			},
			{
				path: '/cases',
				name: 'cases',
				component: () => import('../views/Cases.vue')
			},
			{
				path: '/interface',
				name: 'interface',
				component: () => import('../views/Interface.vue')
			},
			{
				path: '/task',
				name: 'task',
				component: () => import('../views/Task.vue')
			},
			{
				path: '/runlog',
				name: 'runlog',
				component: () => import('../views/RunLog.vue')
			},
			{
				path: '/datanalysis',
				name: 'datanalysis',
				component: () => import('../views/DataAnalysis.vue')
			},
		]
	},
]

const router = new VueRouter({
	routes
})

// 定义路由导航守卫
router.beforeEach((to, from, next) => {
	// to: 表示将要访问的路径
	// from: 代表从哪个路径而来
	// next 是一个函数，表示执行下一步
	// 判断是否访问的是登录页面
	if (to.path === '/login') return next()

	// 判断当前是否能获取token
	if (window.sessionStorage.getItem("token")) return next()
	// 没有token，跳转至首页
	return next('./login')
})

export default router
